package leecode.链表.链表串起来的问题;

import util.ListNode;

/**
 * @author wangxi created on 2024/7/24 23:31
 * @version v1.0
 *
 * https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/description/
 */
public class DeleteDuplicates {

    public static void main(String[] args) {
        int[] nums = {1,1,1,2,3};
        ListNode head = ListNode.createList(nums);

        DeleteDuplicates obj = new DeleteDuplicates();
        obj.deleteDuplicates(head);
    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode first = new ListNode(-1);
        first.next = head;
        ListNode pre = first;
        ListNode cur = head;
        while (cur != null && cur.next != null) {   // && cur.next != null 表示考虑最后一个节点的处理
            int val = cur.val;
            if (cur.next != null && cur.next.val != val) {
                pre = cur;
                cur = cur.next;
            } else {
                while (cur.next != null && cur.next.val == val) {
                    cur = cur.next;
                }
                cur = cur.next;
                pre.next = cur;
            }
        }
        return first.next;
    }
}
